package sk.tuke.android4all.messenger;

import java.util.Date;

import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnGenericMotionListener;
import android.widget.TextView;

public class MessageActivity extends Activity{
	
	private Tts mSpeechEngine = null;
	private String from;
	private long date;
	private String message;
	
	private String selectedTag = null;
	private int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);		
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
		mSpeechEngine = new Tts(getBaseContext());
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    from = extras.getString("from");
		    date = extras.getLong("date");
		    message = extras.getString("message");	
		    
		    setUI();
		}
		
	}
	
	private void setUI(){
		mActionListener = new SwipeListener(this) {
	        @Override
	        public void onDoubleTapAction(int count) {
	            Log.i("TAG", "DoubleTapped " + selectedTag);
	            resetTouchCounter();
	            if(selectedTag != null){
	            	
	            	if(selectedTag.equalsIgnoreCase("Back") ){
	            		finish();
	            	}
	            }
	        }

			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				 
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
		};
		
		View rootView = findViewById(R.id.root);
		rootView.setOnTouchListener(mActionListener);
		
		final TextView receiverView = (TextView) this.findViewById(R.id.message_from_act);
		final TextView dateView = (TextView) this.findViewById(R.id.message_datetime_act);
		final TextView messageView = (TextView) this.findViewById(R.id.message_preview_act);
		final TextView backView = (TextView) this.findViewById(R.id.message_back_act);
		final TextView deleteView = (TextView) this.findViewById(R.id.message_del_act);
		
		receiverView.setText(from);
		dateView.setText(new Date(date).toString());
		messageView.setText(message);
		
		receiverView.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = receiverView.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage("Contact: " + textInControl);
                    selectedTag = receiverView.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		dateView.setOnGenericMotionListener(new OnGenericMotionListener() {

			@Override
			public boolean onGenericMotion(View v, MotionEvent event) {
				mActionListener.resetTouchCounter();
				String textInControl = dateView.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = dateView.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		messageView.setOnGenericMotionListener(new OnGenericMotionListener() {

			@Override
			public boolean onGenericMotion(View v, MotionEvent event) {
				mActionListener.resetTouchCounter();
				String textInControl = messageView.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = messageView.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		deleteView.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View v, MotionEvent event) {
				mActionListener.resetTouchCounter();
				String textInControl = deleteView.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = deleteView.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		backView.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View v, MotionEvent event) {
				mActionListener.resetTouchCounter();
				String textInControl = backView.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = backView.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
	}
}
